<template>
    <section>
        <!-- 工具条 -->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters" ref="filters" size="small">
              <!--  <el-form-item label="重要性" prop="star_level">
                    <el-select clearable placeholder="重要性" v-model="filters.star_level">
                        <el-option :value="-1" label="全部"></el-option>
                        <el-option :key="value" :label="txt" :value="value" v-for="(txt, value) in dict.inspect_importance"></el-option>
                    </el-select>
                    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                        <span>满足条件的数据量：{{filters_count}} 条</span>
                    </el-col>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="searchList">搜索</el-button>
                    <el-button  @click="resetSearch('filters')">重置</el-button>
                </el-form-item>-->

                <el-form-item style="display: block; float: right;">
                    <el-button type="primary" @click="addItem"  size="medium"><i class="el-icon-plus"></i>新增检查项</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <tree-grid @transferFormVisibleIs="handleEdit" @tarnsAddReviseFormIs="addItem" @tarnsSortIs="sortItem"
                   :columns="columns" :tree-structure="true" :data-source="dataSource" :dirver="dirver"></tree-grid>

        <el-col :span="24" class="toolbar_tool"></el-col>
        <!-- =========================== 添加、修改（一级） ===================================== -->
        <el-dialog :title="dialogForm1.title" :visible.sync="dialogForm1.dialogFormVisible" :before-close="closeDialog1" width="560px">
            <inspect-edit :itemForm="itemForm" @saveOneItem="addOrUpdate" @closeDialog="closeDialog1"></inspect-edit>
        </el-dialog>

        <!-- =========================== 添加、修改（二级-下级） ===================================== -->
        <el-dialog :title="dialogForm2.title" :visible.sync="dialogForm2.dialogFormVisible" :before-close="closeDialog2" width="560px">
            <inspect-edit2 :itemForm="itemForm" @saveOneItem2="addOrUpdate2" @closeDialog2="closeDialog2"></inspect-edit2>
        </el-dialog>

        <!-- =========================== 添加、修改（三级-隐患） ===================================== -->
        <el-dialog :title="dialogForm3.title" :visible.sync="dialogForm3.dialogFormVisible" :before-close="closeDialog3" width="560px">
            <inspect-edit3 :itemForm="itemForm" @saveOneItem3="addOrUpdate3" @closeDialog3="closeDialog3"></inspect-edit3>
        </el-dialog>


    </section>
</template>

<script>
    import {TreeGrid} from '../../components/treeTable';
    import * as API from  '../../api/auth_api';
    import * as dat from '../../common/js/date';
    import dict from '../../common/js/dict';
    import {buildTree} from '../../common/js/commons';


    import InspectEdit from './dialog/InspectEdit';
    import InspectEdit2 from './dialog/InspectEdit2';
    import InspectEdit3 from './dialog/InspectEdit3';

    export default {
        components: {
            TreeGrid,
            InspectEdit,
            InspectEdit2,
            InspectEdit3
        },
        data() {
            return {
                dict: dict,
                filters:{
                    star_level: -1
                },
                filters_count: 0,       //显示查询数量item_san_count
                columns: [
                    {text: '检查项', dataIndex: 'item_name'},
                    {text: '检查要点', dataIndex: 'item_main_point'},
                    {text: '重要性', dataIndex: 'star_level'}
                ],
                dataSource: [],
                dataSource_order_no: 0,        //显示序号
                dirver: {},
                dialogForm1: {
                    dialogFormVisible: false,
                    title: '',
                    type: ''
                },
                dialogForm2: {
                    dialogFormVisible: false,
                    title: '',
                    type: ''
                },
                dialogForm3: {
                    dialogFormVisible: false,
                    title: '',
                    type: ''
                },
                itemForm: {},
            }
        },
        methods: {
            getItems() {
                let condition = {};
                // condition.itemLevel = 1;
                condition.star_level = this.filters.star_level;
                API.inspectItemList(condition).then((res) => {
                    if(res.data.status == 200){
                        let datas = JSON.parse(res.data.datas);
                        this.dataSource = datas;
                        this.dataSource_order_no = this.dataSource.length == null ? 1 : (this.dataSource.length + 1);
                        if(this.dataSource && this.dataSource.length > 0) {
                            this.filters_count = this.dataSource[0].item_san_count;
                        }
                    }else{
                        if(res.data.status == 401){
                            this.$router.push('/login');
                        }
                        this.$message.error(res.data.msg);
                    }

                });
            },

            searchList(){
                this.getItems();
            },
            resetSearch(formName){
                this.$refs[formName].resetFields();
                this.searchList();
            },
            addItem(condition) {
                if(condition != undefined && condition.item_level == 1) {  //判断是哪个级别的添加还是修改
                    this.dialogForm2.title = "新增检查项";   //二级
                    this.dialogForm2.type = condition.ttype;
                    this.itemForm = condition;
                    this.dialogForm2.dialogFormVisible = true;
                }else if(condition != undefined && condition.item_level == 2) {
                    this.dialogForm3.title = "新增检查项";   //三级
                    this.dialogForm3.type = condition.ttype;
                    this.itemForm = condition;
                    this.dialogForm3.dialogFormVisible = true;
                }else{
                    this.dialogForm1.title = "新增检查项";
                    this.dialogForm1.type = 1;
                    this.itemForm.item_order_number = this.dataSource_order_no;
                    this.dialogForm1.dialogFormVisible = true;
                }

            },

            handleEdit(condition) {
                /* ============================= 执行删除操作 ========================================== */
                if(condition.ttype == 3){
                    this.handleDelete(condition.id, condition.item_level);
                    return;
                }


                /* ============================= 修改(一二三级)操作 ========================================== */
                if(condition.item_level == 1){
                    this.dialogForm1.title = "编辑检查项";
                    this.itemForm = condition;
                    this.dialogForm1.dialogFormVisible = true;
                    this.dialogForm1.type = condition.ttype;
                }else if(condition.item_level == 2) {
                    this.dialogForm2.title = "编辑检查项";
                    this.itemForm = condition;
                    this.dialogForm2.dialogFormVisible = true;
                    this.dialogForm2.type = condition.ttype;
                }else{
                    this.dialogForm3.title = "编辑检查项";
                    this.itemForm = condition;
                    this.dialogForm3.dialogFormVisible = true;
                    this.dialogForm3.type = condition.ttype;    //判断是添加还是修改
                }


            },
            addOrUpdate(condition) {
                // condition.item_order_number = condition.item_order_number.replace(/\s+/g,"");
                if(this.dialogForm1.type === 1){
                    API.addInspectItem(condition).then((res) => {
                        let result = JSON.parse(res.data.datas);
                        if (res.data.status === 200) {
                            // result["ttype"] = 1;
                            // this.dirver = result;
                            this.searchList();
                            this.$message({type: 'success', message: res.data.msg});
                            this.dataSource_order_no++;
                            this.closeDialog1();
                        }else{
                            if(res.data.status == 401){
                                this.$router.push('/login');
                            }
                            this.$message.error(res.data.msg);
                        }
                    });

                }else{
                    this.handleUpdate(condition,this.dialogForm1);
                    this.closeDialog1();
                }
            },
            addOrUpdate2(row) {

                let condition = {
                    'item_parentid': row.item_parentid,
                    'item_order_number': row.item_order_number,
                    'item_name': row.item_name,
                    'item_level': row.item_level
                };

                if(this.dialogForm2.type === 1){
                    API.addInspectItemTwo(condition).then((res) => {
                        let result = JSON.parse(res.data.datas);
                        if (res.data.status === 200) {
                            result["ttype"] = 1;
                            this.dirver = result;
                            // this.searchList();
                            this.$message({type: 'success', message: res.data.msg});
                            this.closeDialog2();
                        }else{
                            if(res.data.status == 401){
                                this.$router.push('/login');
                            }
                            this.$message.error(res.data.msg);
                        }
                    });

                }else{
                    condition.id = row.id;
                    this.handleUpdate(condition,this.dialogForm2);
                    this.closeDialog2();
                }
            },
            addOrUpdate3(row) {
                let condition = {
                    'item_name': row.item_name,
                    'item_parentid': row.item_parentid,
                    'star_level': row.star_level,
                    'item_main_point': row.item_main_point,
                    'item_order_number': row.item_order_number,
                    'item_level': row.item_level
                };

                if(this.dialogForm3.type === 1){
                    API.addInspectItemThree(condition).then((res) => {
                        let result = JSON.parse(res.data.datas);
                        if (res.data.status === 200) {
                            if(row.is_child_count === "true") {this.searchList();}
                            else {result["ttype"] = 1;this.dirver = result;}

                            this.$message({type: 'success', message: res.data.msg});
                            this.closeDialog3();
                        }else{
                            if(res.data.status == 401){
                                this.$router.push('/login');
                            }
                            this.$message.error(res.data.msg);
                        }
                    });

                }else{
                    condition.id = row.id;
                    delete condition.item_parentid;
                    this.handleUpdate(condition,this.dialogForm3);
                    this.closeDialog3();
                }
            },
            handleUpdate(condition,me) {
                // delete condition.ttype;
                API.updateInspectItem(condition).then((res) => {
                    let result = JSON.parse(res.data.datas);
                    if (res.data.status === 200) {
                        // this.searchList();
                        condition['ttype'] = 2;
                        this.dirver = condition;
                        this.$message({type: 'success', message: res.data.msg});
                        me.dialogFormVisible = false;
                    }else{
                        if(res.data.status == 401){
                            this.$router.push('/login');
                        }
                        this.$message.error(res.data.msg);
                    }
                });
            },
            handleDelete(iid, level) {
                let that = this;
                this.$confirm('此操作将删除该检查项, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    API.removeInspectItem({"id": iid}).then((res) => {
                        if (res.data.status === 200) {
                            if(level == 1) { this.dataSource_order_no--;}
                            this.$message({type: 'success', message: '删除成功!'});
                            // this.searchList();
                            let condition = {
                                "id": iid,
                                'ttype': 3
                            };
                            this.dirver = condition;
                        }else{
                            if(res.data.status == 401){
                                this.$router.push('/login');
                            }
                            this.$message.error(res.data.msg);
                        }
                    });

                }).catch(() => {});
            },

            //排序
            sortItem (condition) {
                let paras = {
                    'before': condition.before,
                    'after': condition.after
                };
                API.sortInspectItem(paras).then((res) => {
                    if (res.data.status === 200) {
                        // this.searchList();
                        condition["ttype"] = 4;
                        this.dirver = condition;
                        this.$message({type: 'success', message: res.data.msg});
                    }else{
                        if(res.data.status == 401){
                            this.$router.push('/login');
                        }
                        this.$message.error(res.data.msg);
                    }
                });
            },


            closeDialog1() {
                this.dialogForm1.dialogFormVisible = false;
                this.itemForm = {};
                this.itemForm.item_order_number = this.dataSource_order_no;
            },
            closeDialog2() {
                this.dialogForm2.dialogFormVisible = false;
                this.itemForm.item_name = '';
            },
            closeDialog3() {
                this.dialogForm3.dialogFormVisible = false;
                this.itemForm.item_name = '';
                this.itemForm.item_main_point = '';
                this.itemForm.star_level = '0';
            },


        },
        mounted(){
            this.$nextTick(()=>this.getItems())
        }
    }
</script>
<style scoped>
    .toolbar_tool{
        padding: 10px;
        margin: 10px 0px;
    }
    .el-table .cell i{display:inline-block}
</style>
